package com.hengxinyongli.iam.filter;

import com.hengxinyongli.iam.exception.IamServiceException;
import com.hengxinyongli.iam.utils.UriUtils;

import java.util.List;
import java.util.Set;

/**
 * @author magic
 * @date 2018/9/28/028 14:34
 * @version 1.0
 * Description PermitsValidateChain
 */
public class PermitsValidateChain {

    private List<PermitsFilter> permitsFilters;
    private int position = 0;

    public PermitsValidateChain(List<PermitsFilter> permitsFilters){
        this.permitsFilters = permitsFilters;
    }

    public void filter(String uri,String method,Set<String> permits){
        if(position >= permitsFilters.size()){
            throw new IamServiceException("无权限访问" + UriUtils.buildValidateUri(uri,method) + "，请联系管理员");
        }
        PermitsFilter filter = permitsFilters.get(position);
        position++;
        filter.invoke(this,uri,method,permits);
    }
}
